#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include "model.h"
#include <fcntl.h>
#include <sys/stat.h>

int power(int x, int y)
{
  if (y >1)
    return(x*power(x,y-1));
  else
    {
      if(y==1)
	return x;
    }
}

data gif2data(char *path)
{
  data ndata;
  char *tmp = malloc(4);
  char *bit = malloc(1);
  
  //size_t
  size_t tmp, tr;
  
  unsigned bit; //stockage bit a bit packet field
  ndata = malloc(sizeof(struct s_data));
  int fp = open(path,O_RDONLY);
  
  int i,j, colortable;
  int n = 0;//nombre de couleur palette
  int tableau[8]; /*donnees du packet field */
  int color[]; //palette yo
  if (fp<0)
    perror ("the game");
  
  //lets redada motherfucking header block fIRSTA/?GIF
  fread(tmp, 1, 3, fp);
  if(strcmp(tmp, "GIF87a"))    
  {
    //logical screen descriptor width
    fread(tmp, 2, 1, fp);
    ndata->w = tmp;
    
    //logical screen descriptor heigth
    fread(tmp, 2, 1, fp);
    ndata->h = tmp;
    
    //packet field
      //bit de poids faible(0):global color table flag
      //bit 1 a 3 : color resolution
      //bit 4 : sort flag
      //bit 5 a 7 : global color table flag
    fread(bit, 1, 1, fp);
    for(i = 0; i<8; i++)
      {
	if(bit & (1u<<i))
	  tableau[i] = 1;
	else
	  tableau[i] = 0;	
      }
    
    //Global color table
    if (tableau[7] == 1)
      {
	for(j=0;j<3;j++)
	  {
	    if(tableau[j] == 1)
	      n = n +j;
	  }
	colortable = 1 << n;
	palette = malloc(3 * colortable);
	//FIXME boucle de la palette
      }
    //Graphics Control Extension we skipping this one 
    fread(tmp, 1, 2, fp);
    if(strcmp(tmp, "21F9"))
      fread(tmp, 1, 6,); // skip des 6 octets
    
  }
}
